package com.tsd.system.controller;

import com.tsd.CommonMsgNameConstants;
import com.tsd.common.controller.BaseController;
import com.tsd.core.datasource.DataSourceContextHolder;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.system.entity.SysSettingExt;
import com.tsd.system.entity.SysUserExt;
import com.tsd.system.service.SysSettingService;
import com.tsd.system.service.WxSettingService;
import com.tsd.system.vo.MiniappSetting;
import com.tsd.system.vo.MpSetting;
import com.tsd.system.vo.OpenSetting;
import com.tsd.system.vo.WxPaySetting;
import com.tsd.wxpay.WxPayConfig4SysSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 描述：第三方配置控制层
 *
 * @author david
 * @date 2021/07/15 10:35:26
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "系统-第三方配置", description = "第三方配置的API描述")
public class SysConfig3rdController extends BaseController {

    @Resource
    private SysSettingService sysSettingService;
    @Resource
    private WxSettingService wxSettingService;
    @Resource
    private WxPayConfig4SysSettingService wxPayConfig4SysSettingService;

    private void changeDataSource(SysSettingExt settingExt) {
        String currentDataSource = DataSourceContextHolder.getDataSourceType();
        if (!HlpUtils.isEmpty(settingExt.getAccount_set_sid())) {
            if (!settingExt.getAccount_set_sid().equalsIgnoreCase(currentDataSource)) {
                DataSourceContextHolder.setDataSourceType(settingExt.getAccount_set_sid());
            }
        }
    }

    @ApiOperation(value = "获取第三方配置")
    @ResponseBody
    @RequestMapping(value = "/getSysSetting3rd.action", method = RequestMethod.POST)
    public HttpResult<Object> getSysSetting3rd() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysSettingExt params = super.getParams(request, SysSettingExt.class);
            this.changeDataSource(params);
            super.checkEmpty(params.getCode(), CommonMsgNameConstants.EMPTY_MSG_CODE);
            SysSettingExt settingExt = sysSettingService.getSysSettingByCodeOwnerSid(params);
            if (settingExt == null) {
                settingExt = params;
            }
            hr.setData(settingExt);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "保存第三方配置")
    @ResponseBody
    @RequestMapping(value = "/saveSysSetting3rd.action", method = RequestMethod.POST)
    public HttpResult<Object> saveSysSetting3rd() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUserExt opUser = super.getSysUser();
            SysSettingExt params = super.getParams(request, SysSettingExt.class);
            this.changeDataSource(params);
            super.checkEmpty(params.getCode(), CommonMsgNameConstants.EMPTY_MSG_CODE);
            sysSettingService.saveSysSetting(params, opUser);
            if (OpenSetting.SETTING_CODE_WX_OPEN.equals(params.getCode())) {
                wxSettingService.clearWxOpenCache();
            } else if (MpSetting.SETTING_CODE_WX_MP.equals(params.getCode())) {
                wxSettingService.clearWxMpCache();
            } else if (MiniappSetting.SETTING_CODE_WX_MINIAPP.equals(params.getCode())) {
                wxSettingService.clearWxMiniappCache();
            } else if (WxPaySetting.SETTING_CODE_WX_PAY.equals(params.getCode())) {
                wxSettingService.clearWxPaySettingCache();
                wxPayConfig4SysSettingService.clearWxPayConfig(DataSourceContextHolder.getDataSourceType());
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }
}
